package com.castillo.exam;
/**
 * 
 */




import java.util.List;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;



public class ProviderDAO implements ProviderDAOInterface {

	private Session session;
	

	/**
	 * default constructor
	 */
	public ProviderDAO () {
		session = HibernateSession.getSession();
	}
	
	/* 
	 * selects one provider by Id
	 * @param id
	 * @return Advert
	 */
	public Provider selectById(int id) {
	    Provider provider = (Provider) session.get(Provider.class, id);
	    return provider;
	}
	

	/*
	 * retrieves all providers from db
	 * @return List of providers
	 */
	public List<Provider> selectAll() {
		List<Provider> providers = null;
		try {
			String hql = "from Provider";
			providers = session.createQuery(hql)
			.list();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return providers;
	}
	/*
	 * inserts a new provider in database
	 * retrieves generated id and sets to provider instance
	 * @param new provider
	 */
	public void insert(Provider provider) {
	    session.beginTransaction();
	 
	    Integer id = (Integer) session.save(provider);
	    provider.setId(id);
	         
	    session.getTransaction().commit();
	         
	}


	public void saveOrUpdate(Provider provider) {
	    session.beginTransaction();
	    
	    session.saveOrUpdate(provider);
	 
	    session.getTransaction().commit();
		
	}

	@Override
	public void update(Provider provider) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void delete(Provider provider) {
		// TODO Auto-generated method stub
		
	}

}
